/// 积分变更历史记录数据模型
class PointsHistoryRecord {
  final int id;
  final int userid;
  final int pointsChange;
  final int pointsBefore;
  final int pointsAfter;
  final int changeType;
  final String changeReason;
  final String? relatedId;
  final String operator;
  final String? remark;
  final String status;
  final String createdAt;

  PointsHistoryRecord({
    required this.id,
    required this.userid,
    required this.pointsChange,
    required this.pointsBefore,
    required this.pointsAfter,
    required this.changeType,
    required this.changeReason,
    this.relatedId,
    required this.operator,
    this.remark,
    required this.status,
    required this.createdAt,
  });

  /// 从JSON创建实例
  factory PointsHistoryRecord.fromJson(Map<String, dynamic> json) {
    return PointsHistoryRecord(
      id: json['id'] ?? 0,
      userid: json['userid'] ?? 0,
      pointsChange: json['points_change'] ?? 0,
      pointsBefore: json['points_before'] ?? 0,
      pointsAfter: json['points_after'] ?? 0,
      changeType: json['change_type'] ?? 0,
      changeReason: json['change_reason'] ?? '',
      relatedId: json['related_id']?.toString(),
      operator: json['operator']?.toString() ?? '',
      remark: json['remark'],
      status: json['status'] ?? '',
      createdAt: json['created_at'] ?? '',
    );
  }

  /// 转换为JSON
  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'userid': userid,
      'points_change': pointsChange,
      'points_before': pointsBefore,
      'points_after': pointsAfter,
      'change_type': changeType,
      'change_reason': changeReason,
      'related_id': relatedId,
      'operator': operator,
      'remark': remark,
      'status': status,
      'created_at': createdAt,
    };
  }

  /// 获取积分变动类型文本
  String get changeTypeText {
    switch (changeType) {
      case 1:
        return '签到';
      case 2:
        return '购物';
      case 3:
        return '活动奖励';
      case 4:
        return '积分兑换';
      case 5:
        return '管理员调整';
      case 6:
        return '系统消耗';
      default:
        return '其他';
    }
  }

  /// 获取积分变动颜色
  bool get isIncrease => pointsChange > 0;
  bool get isDecrease => pointsChange < 0;

  /// 格式化积分变更显示
  String get formattedPoints {
    if (pointsChange > 0) {
      return '+${pointsChange}积分';
    } else {
      return '${pointsChange}积分';
    }
  }

  /// 获取简化的变动原因
  String get shortReason {
    if (changeReason.length > 20) {
      return '${changeReason.substring(0, 20)}...';
    }
    return changeReason;
  }

  /// 格式化日期显示
  String get formattedDate {
    try {
      final date = DateTime.parse(createdAt);
      return '${date.year}-${date.month.toString().padLeft(2, '0')}-${date.day.toString().padLeft(2, '0')} ${date.hour.toString().padLeft(2, '0')}:${date.minute.toString().padLeft(2, '0')}';
    } catch (e) {
      return createdAt;
    }
  }

  /// 获取简化日期（仅显示月日时分）
  String get simplifiedDate {
    try {
      final date = DateTime.parse(createdAt);
      return '${date.month}/${date.day} ${date.hour.toString().padLeft(2, '0')}:${date.minute.toString().padLeft(2, '0')}';
    } catch (e) {
      return createdAt;
    }
  }

  @override
  String toString() {
    return 'PointsHistoryRecord(id: $id, pointsChange: $pointsChange, changeType: $changeType, changeReason: $changeReason)';
  }
}

/// 积分变更历史响应模型
class PointsHistoryResponse {
  final String status;
  final String message;
  final int total;
  final int page;
  final int pageSize;
  final int currentPoints;
  final List<PointsHistoryRecord> records;

  PointsHistoryResponse({
    required this.status,
    required this.message,
    required this.total,
    required this.page,
    required this.pageSize,
    required this.currentPoints,
    required this.records,
  });

  /// 从JSON创建实例
  factory PointsHistoryResponse.fromJson(Map<String, dynamic> json) {
    final dataMap = json['data'] as Map<String, dynamic>? ?? {};
    final recordsJson = dataMap['records'] as List<dynamic>? ?? [];
    
    return PointsHistoryResponse(
      status: json['status'] ?? 'error',
      message: json['message'] ?? '未知错误',
      total: dataMap['total'] ?? 0,
      page: dataMap['page'] ?? 1,
      pageSize: dataMap['page_size'] ?? 10,
      currentPoints: dataMap['current_points'] ?? 0,
      records: recordsJson.map((json) => PointsHistoryRecord.fromJson(json)).toList(),
    );
  }

  /// 是否成功
  bool get isSuccess => status == 'success';

  /// 错误信息
  String get errorMessage => isSuccess ? '' : message;

  @override
  String toString() {
    return 'PointsHistoryResponse(status: $status, total: $total, records: ${records.length})';
  }
}
